package com.yuhong.springbootinit.model.vo;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuhong.springbootinit.model.entity.Position;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 帖子视图
 *
 * @author 任玉洪</a>

 */
@Data
public class PositionVO implements Serializable {

    private Long id;


    /**
     * 职位的唯一id
     */
    private String positionId;

    /**
     * 职位名称
     */
    private String title;

    /**
     * 薪资
     */
    private Integer salary;

    /**
     * base地
     */
    private String location;

    /**
     * 哪届
     */
    private String gen;

    /**
     * 教育背景
     */
    private String bg;

    /**
     * 需求列表（JSON 格式） "[\"apple\", \"banana\", \"cherry\"]"
     */
    private List<String> requirement;

    /**
     * 描述
     */
    private String description;

    /**
     * 开放时间
     */
    private Date startDate;

    /**
     * 结束时间
     */
    private Date endDate;

    /**
     * 职位状态
     */
    private Integer status;

    /**
     * 职位标签列表 "[\"apple\", \"banana\", \"cherry\"]"
     */
    private List<String> tags;

    /**
     * 包装类转对象
     *
     * @param positionVO
     * @return
     */
    public static Position voToObj(PositionVO positionVO) {
        if (positionVO == null) {
            return null;
        }
        Position position = new Position();
        BeanUtils.copyProperties(positionVO, position);
        List<String> tagList = positionVO.getTags();
        position.setTags(JSONUtil.toJsonStr(tagList));
        position.setRequirement(JSONUtil.toJsonStr(positionVO.getRequirement()));
        return position;
    }

    /**
     * 对象转包装类
     *
     * @param position
     * @return
     */
    public static PositionVO objToVo(Position position) throws JsonProcessingException {
        if (position == null) {
            return null;
        }
        PositionVO positionVO = new PositionVO();
        BeanUtils.copyProperties(position, positionVO);

        ObjectMapper objectMapper = new ObjectMapper();
        List<String> requirementList = objectMapper.readValue(position.getRequirement(),objectMapper.getTypeFactory().constructCollectionType(List.class, String.class));
        List<String> tagList = objectMapper.readValue(position.getTags(),objectMapper.getTypeFactory().constructCollectionType(List.class, String.class));

        positionVO.setRequirement(requirementList);
        positionVO.setTags(tagList);

        return positionVO;
    }
}
